package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryBizAppSceneRelListReq;
import com.sg.dto.base.framework.req.QueryBizAppSceneRelListReq;
import com.sg.entity.BizAppSceneRelationship;
import com.sg.service.base.framework.MBizAppSceneRelationshipService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.BizAppSceneRelationshipMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MBizAppSceneRelationshipServiceImpl
        extends ServiceImpl<BizAppSceneRelationshipMapper, BizAppSceneRelationship>
        implements MBizAppSceneRelationshipService {

    @Autowired
    BizAppSceneRelationshipMapper bizAppSceneRelationshipMapper;

    /**
     * 1-1-08查询应用赋能场景列表[1418]
     * gen by moon at 8/15/2022, 10:23:59 PM
     */
    @Trace(operationName = "1-1-08查询应用赋能场景列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<BizAppSceneRelationship> queryBizAppSceneRelList(QueryBizAppSceneRelListReq reqDto) {

        try {
            QueryWrapper<BizAppSceneRelationship> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(BizAppSceneRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(BizAppSceneRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(BizAppSceneRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<BizAppSceneRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-08批量查询应用赋能场景列表[36]
     * gen by moon at 8/15/2022, 10:23:59 PM
     */
    @Trace(operationName = "1-1-08批量查询应用赋能场景列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<BizAppSceneRelationship> batchQueryBizAppSceneRelList(BatchQueryBizAppSceneRelListReq reqDto) {

        try {
            QueryWrapper<BizAppSceneRelationship> queryWrapper = new QueryWrapper<>();

            if (reqDto.getBizAppSceneRelList() == null || reqDto.getBizAppSceneRelList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("biz_scene_id", reqDto.getBizAppSceneRelList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(BizAppSceneRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(BizAppSceneRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(BizAppSceneRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<BizAppSceneRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-1-08删除应用赋能场景关系[35]
     * gen by moon at 8/15/2022, 10:23:59 PM
     */
    @Trace(operationName = "1-1-08删除应用赋能场景关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBizAppSceneRel(String bizAppSceneRelationshipId) {

        return super.removeById(bizAppSceneRelationshipId);
    }

    /**
     * 1-1-08修改应用赋能场景关系	[34]
     * gen by moon at 8/15/2022, 10:23:58 PM
     */
    @Trace(operationName = "1-1-08修改应用赋能场景关系	")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBizAppSceneRel(BizAppSceneRelationship bizAppSceneRelationship) {

        return super.updateById(bizAppSceneRelationship);
    }

    /**
     * 1-1-08新增应用赋能场景[33]
     * gen by moon at 8/15/2022, 10:23:58 PM
     */
    @Trace(operationName = "1-1-08新增应用赋能场景")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addBizAppSceneRel(BizAppSceneRelationship bizAppSceneRelationship) {
        bizAppSceneRelationship.setBizAppSceneRelationshipId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        bizAppSceneRelationship.setIsArchive("FALSE");
        bizAppSceneRelationship.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        bizAppSceneRelationship.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (bizAppSceneRelationship != null && bizAppSceneRelationship.getSubjectLifeCycle() != null && bizAppSceneRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            bizAppSceneRelationship.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        bizAppSceneRelationship.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        bizAppSceneRelationship.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (bizAppSceneRelationship != null && bizAppSceneRelationship.getSubjectLifeCycle() != null && bizAppSceneRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            bizAppSceneRelationship.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        bizAppSceneRelationship.setIsValid("FALSE");
        bizAppSceneRelationship.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        bizAppSceneRelationship.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(bizAppSceneRelationship)) {
            return bizAppSceneRelationship.getBizAppSceneRelationshipId();
        } else {
            return "-1";
        }

    }
}
